import pymysql


class Connection(object):
    def __init__(self):
        self.conn = pymysql.connect(host ="192.168.199.132",port=3306, user = "root", password = "mysql", database = "py31")

    def __enter__(self):
        return self.conn

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("执行到这里了")
        self.conn.close()

if __name__ == '__main__':
    with Connection() as conn:
        cur = conn.cursor()
        cur.execute("select * from students")
        rst = cur.fetchall()
        print(rst)
        cur.close()